<h2>Sales Register</h2>

<?php
    $this->headTitle('Sales Registry');
    $acl = $this->acl;
?>

<?php if ($acl->isAllowed($this->currentUser, 'access finance pages')): ?>
<a href="<?php echo $this->url(
    array(
        'module' => 'finance',
        'controller' => 'reports',
        'action' => 'index',
    ), 'default', true
);?>">Reports</a> |
<?php endif; ?>

<?php if ($acl->isAllowed($this->currentUser, 'access finance pages')): ?>
<a href="<?php echo $this->url(
    array(
        'module' => 'finance',
        'controller' => 'reports',
        'action' => 'csvexportsalesregistry',
        'date' => $this->date
    ), 'default', true
);?>">Export to csv</a> |
<?php endif; ?>

<?php if ($acl->isAllowed($this->currentUser, 'access finance pages')): ?>
<a href="<?php echo $this->url(
    array(
        'module' => 'finance',
        'controller' => 'reports',
        'action' => 'pdfsalesregister',
    ), 'default', true
);?>">Export to pdf</a> |
<?php endif; ?>

<fieldset class="fieldset_properties">
    <legend>
        <img src="/images/design/search.png"></li>
                     Search
     </legend>
        <?php echo $this->form; ?>
</fieldset>


<table class="data_table">
    <tr>
        <th>Date</th>
        <th>Particulars</th>
        <th>Type</th>
        <th>Id</th>
        <th>Debit Amount</th>
        <th>Credit Amount</th>
    </tr>
<?php
    $debitTotal = 0;
    $creditTotal = 0;
    $invoiceDetails = $this->invoiceDetails;
    for($invoice = 0; $invoice <= sizeof($invoiceDetails)-1; $invoice += 1) {
?>
    <tr>
        <td>
            <?php echo $this->escape($this->timestampToHuman(
                                        $invoiceDetails[$invoice]['date'])); ?>
        </td>
        <td>
            <?php 
                if ($invoiceDetails[$invoice]['to_type'] == 
                    Core_Model_Invoice::TO_TYPE_ACCOUNT) {
                    echo 'Account';
                } else {
                    echo "Contact";
                }
                echo " <br />";
                echo $this->partyHyperlink($invoiceDetails[$invoice]['to_type'], 
                                      $invoiceDetails[$invoice]['to_type_id']);
            ?>
        </td>
        <td> Invoice </td>
        <td>
        <a href="<?php echo $this->url(
            array(
                'module' => 'default',
                'controller' => 'invoice',
                'action' => 'viewdetails',
                'invoice_id' => $invoiceDetails[$invoice]['invoice_id'],
            ), 'default', true
        ); ?>"><?php echo $invoiceDetails[$invoice]['invoice_id'];?></a>
        </td>
        
       <?php
         $invoiceModel = new Core_Model_Invoice(
                                    $invoiceDetails[$invoice]['invoice_id']);
         $totalAmount = $invoiceModel->getTotalAmount();
         $debitTotal +=  $totalAmount;
         echo "<td class='text_align_right'>".$totalAmount."</td>"; 
         $items = $invoiceModel->getItems();
         $totalPrice = 0;
         $tax_types = array ();
         $ledgerEntryIds = array ();
         $taxTypeModel = new Core_Model_Tax_Type;
         for($i = 0; $i <= sizeof($items)-1; $i += 1) {
           if($items[$i]['tax_type_id'] != '0') {
                $taxName = 
                     $taxTypeModel->getTaxNameFromId($items[$i]['tax_type_id']);
                if (!(in_array($taxName, $tax_types))) { 
                    array_push($tax_types, $taxName);
                }
            }
         }
        echo "<td class='text_align_right'>";
        
        $invoiceRecord = $invoiceModel->fetch();
        for ($tax = 0; $tax <= sizeof($tax_types)-1; $tax += 1) {
            $totalTaxAmount = 0;
            for($i = 0; $i <= sizeof($items)-1; $i += 1) {
                $taxName = 
                     $taxTypeModel->getTaxNameFromId($items[$i]['tax_type_id']);
                if($taxName == $tax_types[$tax])
                {
                   if ($invoiceRecord['invoice_type'] == 1) {
                            $items[$i]['quantity'] = 1;
                            $items[$i]['unit_price'] = $items[$i]['amount'];
                    }
                   $price = $items[$i]['unit_price'] * $items[$i]['quantity'];
                   $taxPercentage = $taxTypeModel->getTaxPercentageFromId(
                                                     $items[$i]['tax_type_id']);
                   $taxAmount = ($price * $taxPercentage) /  100 ;
                   $totalTaxAmount = $totalTaxAmount + $taxAmount;                   
                }
            }
            if ($tax_types[$tax]) {
                echo $tax_types[$tax]." - ".$totalTaxAmount."<br/>";
            }
            else {
                 echo $totalTaxAmount."<br/>";
            }
            $creditTotal += $totalTaxAmount;
         }
         echo "</td>";
       ?>
       
 <?php } //end of for invoice loop?>
 
 
 <?php
    $salesReturnDetails = $this->salesReturnDetails;
    for($salesReturn = 0; $salesReturn <= sizeof($salesReturnDetails)-1; 
                                                     $salesReturn += 1) {
 ?>
     <tr>
        <td>
            <?php echo $this->escape($this->timestampToHuman(
                                $salesReturnDetails[$salesReturn]['date'])); ?>
        </td>
        <td>
          <?php 
                $invoiceModel = new Core_Model_Invoice(
                        $salesReturnDetails[$salesReturn]['invoice_id']);
                $invoiceRecord = $invoiceModel->fetch();
                if ($invoiceRecord['to_type'] == 
                    Core_Model_Invoice::TO_TYPE_ACCOUNT) {
                    echo 'Account';
                } else {
                    echo "Contact";
                }
                echo " <br />";
                echo $this->partyHyperlink($invoiceRecord['to_type'], 
                                        $invoiceRecord['to_type_id']);
           ?>
        </td>
        <td> Sales Return </td>
        <td>
        <a href="<?php echo $this->url(
            array(
                'module' => 'default',
                'controller' => 'salesreturn',
                'action' => 'viewdetails',
                'sales_return_id' => 
                        $salesReturnDetails[$salesReturn]['sales_return_id'],
            ), 'default', true
        ); ?>"><?php echo $this->escape(
                $salesReturnDetails[$salesReturn]['sales_return_id']); ;?></a>
        </td>
        
       <?php
         $salesReturnModel = new Core_Model_SalesReturn(
                        $salesReturnDetails[$salesReturn]['sales_return_id']);
         $totalAmount = $salesReturnModel->getTotalAmount();
         $debitTotal +=  $totalAmount;
         echo "<td class='text_align_right'>".$totalAmount."</td>"; 
         $items = $salesReturnModel->getItems();
         $totalPrice = 0;
         $tax_types = array ();
         $ledgerEntryIds = array ();
         $taxTypeModel = new Core_Model_Tax_Type;
         for($i = 0; $i <= sizeof($items)-1; $i += 1) {
           if($items[$i]['tax_type_id'] != '0') {
                $taxName = 
                     $taxTypeModel->getTaxNameFromId($items[$i]['tax_type_id']);
                if (!(in_array($taxName, $tax_types))) { 
                    array_push($tax_types, $taxName);
                }
            }
         }
        echo "<td class='text_align_right'>";
        for ($tax = 0; $tax <= sizeof($tax_types)-1; $tax += 1) {
            $totalTaxAmount = 0;
            for($i = 0; $i <= sizeof($items)-1; $i += 1) {
                $taxName = 
                     $taxTypeModel->getTaxNameFromId($items[$i]['tax_type_id']);
                if($taxName == $tax_types[$tax])
                {
                   $price = $items[$i]['unit_price'] * $items[$i]['quantity'];
                   $taxPercentage = $taxTypeModel->getTaxPercentageFromId(
                                                     $items[$i]['tax_type_id']);
                   $taxAmount = ($price * $taxPercentage) /  100 ;
                   $totalTaxAmount = $totalTaxAmount + $taxAmount;                   
                }
            }
            if ($tax_types[$tax]) {
                echo $tax_types[$tax]." - ".$totalTaxAmount."<br/>";
            }
            else {
                 echo $totalTaxAmount."<br/>";
            }
           
            $creditTotal += $totalTaxAmount;
         }
         echo "</td>";
       ?>
       
 <?php } //end of for salesReturn loop?>
 
</table>

<?php
    echo "Total Debit = <b>".$debitTotal."</b><br/>";
    echo "Total Credit = <b>".$creditTotal."</b>";
?>
